{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Convert Integer LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: convertInteger"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #整数转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>整数转换。编写一个函数，确定需要改变几个位才能将整数A转成整数B。</p>\n",
    "\n",
    "<p> <strong>示例1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong> 输入</strong>：A = 29 （或者0b11101）, B = 15（或者0b01111）\n",
    "<strong> 输出</strong>：2\n",
    "</pre>\n",
    "\n",
    "<p> <strong>示例2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong> 输入</strong>：A = 1，B = 2\n",
    "<strong> 输出</strong>：2\n",
    "</pre>\n",
    "\n",
    "<p> <strong>提示:</strong></p>\n",
    "\n",
    "<ol>\n",
    "<li>A，B范围在[-2147483648, 2147483647]之间</li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [convert-integer-lcci](https://leetcode.cn/problems/convert-integer-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [convert-integer-lcci](https://leetcode.cn/problems/convert-integer-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A&0xffffffff)^(B&0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            if (A & (1 << i)) != (B & (1 << i)):\n",
    "                res += 1\n",
    "            A >> 1\n",
    "            B >> 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        def hamming_weight(num: int):\n",
    "            count = 0\n",
    "            while num:\n",
    "                count += 1\n",
    "                num &= num -1\n",
    "            return count\n",
    "        k = 32\n",
    "        if A < 0:\n",
    "            A = -A\n",
    "            for i in range(k):\n",
    "                A ^= 1 << i\n",
    "            A += 1\n",
    "        if B < 0:\n",
    "            B = -B\n",
    "            for i in range(k):\n",
    "                B ^= 1 << i\n",
    "            B += 1\n",
    "        \n",
    "        return hamming_weight(A ^ B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        C = A ^ B\n",
    "        count = 0\n",
    "        for i in range(32):\n",
    "            count += C >> i & 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, a: int, b: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            if a & (1<<i) != b & (1 << i):\n",
    "                ans += 1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        C = A^B\n",
    "        print(C)\n",
    "        for i in range(32):\n",
    "            if (C>>i)&1==1:\n",
    "                res+=1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def convertInteger(self, A: int, B: int) -> int:\n",
    "#         check=1\n",
    "#         outnum=0\n",
    "#         # print(bin(A),bin(B))\n",
    "#         # A=A& 0xffffffff\n",
    "#         # B=B& 0xffffffff \n",
    "#         while check<=A or check<=B:\n",
    "#             print(\"1\")\n",
    "#             if check&A != check&B:\n",
    "#                 outnum+=1\n",
    "#             check<<=1\n",
    "\n",
    "#         return outnum\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        check=1\n",
    "        outnum=0\n",
    "        # print(bin(A),bin(B))\n",
    "\n",
    "        while check<=2147483648:\n",
    "\n",
    "            if check&A != check&B:\n",
    "                outnum+=1\n",
    "            check<<=1\n",
    "\n",
    "        return outnum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        # binA=bin(A)\n",
    "        # binB=bin(B)\n",
    "        # S=binA[2:] ^binB[2:]\n",
    "        return bin( (A&0xffffffff) ^ (B&0xffffffff)).count('1')\n",
    "        # return bin( (A) ^ (B)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        cnt = 0\n",
    "        mask = 1 << 31\n",
    "        while mask:\n",
    "            if (A & mask) ^ (B & mask):\n",
    "                cnt += 1\n",
    "            mask >>= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        ans = 0\n",
    "        c = A^B\n",
    "        for i in range(32):\n",
    "            if c >> i & 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        xor = (A ^ B) & ((1<<32)-1)\n",
    "        cnt = 0\n",
    "        while xor != 0:\n",
    "            cnt += 1\n",
    "            xor &= xor-1\n",
    "        \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        A&=0xffffffff\n",
    "        B&=0xffffffff\n",
    "        return (A^B).bit_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            res += (A>>i & 1)^(B>>i & 1)\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "       \n",
    "        return bin((A & 0xffffffff)^(B & 0xffffffff)).count('1')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        \n",
    "        c2 = bin((A & 0xffffffff)^(B & 0xffffffff))\n",
    "        return c2.count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "\n",
    "        A = A & 0xFFFFFFFF\n",
    "        B = B & 0xFFFFFFFF\n",
    "        temp = A ^ B\n",
    "\n",
    "\n",
    "        return str(bin(temp)).count('1')\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A&0xffffffff)^(B&0xffffffff)).count(\"1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            res += (A>>i & 1)^(B>>i & 1)\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        # binA=bin(A)\n",
    "        # binB=bin(B)\n",
    "        # S=binA[2:] ^binB[2:]\n",
    "       return bin( (A&0xffffffff) ^ (B&0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        mask = A ^ B\n",
    "        if mask < 0:\n",
    "            mask += 2**32\n",
    "        num_bits = 0\n",
    "        while mask != 0:\n",
    "            num_bits += mask % 2\n",
    "            mask //= 2\n",
    "        return num_bits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        #return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')\n",
    "        c = (A & 0xffffffff) ^ (B & 0xffffffff)\n",
    "        res = 0\n",
    "        while c:\n",
    "            res += 1\n",
    "            c &= c - 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        cntA = 0\n",
    "        cntB = 0\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            cntA = A & (1 << i)\n",
    "            cntB = B & (1 << i)\n",
    "            if cntA != cntB:\n",
    "                res += 1\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        ans=0\n",
    "        c=A^B\n",
    "        for i in range(32):\n",
    "            ans+=c>>i & 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        n = 0xffffffff\n",
    "        return bin((A & n) ^ (B & n)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        # A &= 0xffffffff\n",
    "        # B &= 0xffffffff\n",
    "        # c = A ^ B\n",
    "        # c = bin(c)[2:] if c < 0 else bin(c & 0xffffffff)[2:]\n",
    "        # trans_qty = 0\n",
    "        # for i in range(len(c)):\n",
    "        #     if c[i] == '1':\n",
    "        #         trans_qty += 1\n",
    "        # return trans_qty\n",
    "\n",
    "        # A &= 0xffffffff\n",
    "        # B &= 0xffffffff\n",
    "        # c = A ^ B\n",
    "        # trans_qty = 0\n",
    "        # while c:\n",
    "        #     if c & 1:\n",
    "        #         trans_qty += 1\n",
    "        #     c >>= 1\n",
    "        # return trans_qty\n",
    "\n",
    "        cnt = 0\n",
    "        for i in range(32):\n",
    "            if (A & (1 << i)) != (B & (1 << i)): cnt += 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        c = A ^ B\n",
    "        for i in range(32):\n",
    "            res += c >> i & 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        # return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')\n",
    "        a = str(bin(A & 0xffffffff))[2:]\n",
    "        b = str(bin(B & 0xffffffff))[2:]\n",
    "        if len(a) > len(b):\n",
    "            b = b.zfill(len(a))\n",
    "        elif len(a) < len(b):\n",
    "            a = a.zfill(len(b))\n",
    "        ret = 0\n",
    "        for i in range(0, len(a)):\n",
    "            if a[i] != b[i]:\n",
    "                ret += 1\n",
    "        return ret\n",
    "        # print(bin(A & 0xffffffff))\n",
    "        # print(bin(B & 0xffffffff))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        c = A^B\n",
    "        for i in range(32):\n",
    "            res += c>>i&1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        c = A^B\n",
    "        for i in range(32):\n",
    "            if c>>i & 1:\n",
    "                res+= 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def help(self, A, B):\n",
    "        if A < 0:           \n",
    "            rba = ''\n",
    "            A = -A      \n",
    "            ba = bin(A)[2:].zfill(32)      \n",
    "            for i in ba:\n",
    "                if i == '0':\n",
    "                    rba += '1'\n",
    "                else:\n",
    "                    rba += '0'\n",
    "            fba = ''\n",
    "            rrba = rba[::-1]\n",
    "            for j in range(len(rrba)):\n",
    "                if rrba[j] == '0':\n",
    "                    fba += '1' + rrba[j + 1:] \n",
    "                    break\n",
    "                else:\n",
    "                    fba += '0'\n",
    "                    \n",
    "            ba = fba[::-1]\n",
    "        else:\n",
    "            ba = bin(A)[2:].zfill(32)\n",
    "\n",
    "        return ba\n",
    "\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "          ba,bb = '', ''\n",
    "           \n",
    "          if A < 0:\n",
    "              ba = bin(A & 0xffffffff)[2:]\n",
    "          else:\n",
    "              ba = bin(A)[2:].zfill(32)\n",
    "              \n",
    "          if B < 0:\n",
    "              bb = bin(B & 0xffffffff)[2:]\n",
    "          else:\n",
    "              bb = bin(B)[2:].zfill(32)\n",
    "          \n",
    "    \n",
    "          cnt = 0\n",
    "          for i in range(len(ba)):\n",
    "            if ba[i] != bb[i]:\n",
    "                cnt += 1\n",
    "          return cnt\n",
    "      \n",
    "        \n",
    "             \n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def help(self, A, B):\n",
    "        if A < 0:           \n",
    "            rba = ''\n",
    "            A = -A      \n",
    "            ba = bin(A)[2:].zfill(32)      \n",
    "            for i in ba:\n",
    "                if i == '0':\n",
    "                    rba += '1'\n",
    "                else:\n",
    "                    rba += '0'\n",
    "            fba = ''\n",
    "            rrba = rba[::-1]\n",
    "            for j in range(len(rrba)):\n",
    "                if rrba[j] == '0':\n",
    "                    fba += '1' + rrba[j + 1:] \n",
    "                    break\n",
    "                else:\n",
    "                    fba += '0'\n",
    "                    \n",
    "            ba = fba[::-1]\n",
    "        else:\n",
    "            ba = bin(A)[2:].zfill(32)\n",
    "\n",
    "        return ba\n",
    "\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "          ba,bb = '', ''\n",
    "           \n",
    "          if A < 0:\n",
    "              ba = bin(A & 0xffffffff)[2:]\n",
    "          else:\n",
    "              ba = bin(A)[2:].zfill(32)\n",
    "              \n",
    "          if B < 0:\n",
    "              bb = bin(B & 0xffffffff)[2:]\n",
    "          else:\n",
    "              bb = bin(B)[2:].zfill(32)\n",
    "          \n",
    "    \n",
    "          cnt = 0\n",
    "          for i in range(len(ba)):\n",
    "            if ba[i] != bb[i]:\n",
    "                cnt += 1\n",
    "          return cnt\n",
    "      \n",
    "        \n",
    "             \n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        ans = 0\n",
    "        A = A ^ B\n",
    "        for i in range(32):\n",
    "            if A & 1<<i:\n",
    "                ans += 1\n",
    "        return  ans      \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        A=A&0Xffffffff\n",
    "        B=B&0Xffffffff\n",
    "\n",
    "        A = bin(A)[2:]\n",
    "        B = bin(B)[2:]\n",
    "\n",
    "        if len(A) < 32:\n",
    "            A = '0' * (32 - len(A)) + A\n",
    "        if len(B) < 32:\n",
    "            B = '0' * (32 - len(B)) + B\n",
    "        \n",
    "        ret = 0\n",
    "        for i in range(32):\n",
    "            if A[i] != B[i]:\n",
    "                ret += 1\n",
    "        \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')\n",
    "\n",
    "        bA = bin(A & 0xffffffff)[2:].zfill(32)\n",
    "        bB = bin(B & 0xffffffff)[2:].zfill(32)\n",
    "        # print(bA, bB)\n",
    "        return sum(a != b for a, b in zip(bA, bB))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        num=0\n",
    "        for i in range(32):\n",
    "            a=A&(1<<i)\n",
    "            b=B&(1<<i)\n",
    "            num+=a!=b\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A^B)& 0xffffffff).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        # A &= 0xffffffff\n",
    "        # B &= 0xffffffff\n",
    "        # c = A ^ B\n",
    "        # c = bin(c)[2:] if c < 0 else bin(c & 0xffffffff)[2:]\n",
    "        # trans_qty = 0\n",
    "        # for i in range(len(c)):\n",
    "        #     if c[i] == '1':\n",
    "        #         trans_qty += 1\n",
    "        # return trans_qty\n",
    "        A &= 0xffffffff\n",
    "        B &= 0xffffffff\n",
    "        c = A ^ B\n",
    "        trans_qty = 0\n",
    "        while c:\n",
    "            if c & 1:\n",
    "                trans_qty += 1\n",
    "            c >>= 1\n",
    "        return trans_qty\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "\n",
    "        bA = bin(A & 0xffffffff)[2:].zfill(32)\n",
    "        bB = bin(B & 0xffffffff)[2:].zfill(32)\n",
    "        # print(bA, bB)\n",
    "        return sum(a != b for a, b in zip(bA, bB))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        cnt=0\n",
    "        for i in range(32):\n",
    "            if (A&(1<<i))!=(B&(1<<i)):\n",
    "                cnt+=1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def convertInteger(self, A: int, B: int) -> int:\n",
    "#         A=A&0xFFFFFFFF\n",
    "#         B=B&0xFFFFFFFF\n",
    "#         return (A^B).bit_count()\n",
    "\n",
    "# ### 网：方法1\n",
    "# class Solution:\n",
    "#     def convertInteger(self,A,B):\n",
    "#         return bin((A&0xFFFFFFFF)^(B&0xFFFFFFFF)).count(\"1\") ### or str.bit_count()\n",
    "\n",
    "### 网：方法3\n",
    "class Solution:\n",
    "    def convertInteger(self,A,B):\n",
    "        C=(A&0xFFFFFFFF)^(B&0xFFFFFFFF) \n",
    "        res=0\n",
    "        while C:\n",
    "            C=C&(C-1)\n",
    "            res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        count=0\n",
    "        h1=A^B\n",
    "        for i in range(32):\n",
    "            count+=h1>>i&1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff))[2:].count('1')\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        mask = A ^ B\n",
    "        mask &= (1 << 32) - 1\n",
    "        count = 0\n",
    "        while mask:\n",
    "            mask &= mask - 1\n",
    "            count += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        A ^= B\n",
    "        cnt = 0\n",
    "        if A < 0:   \n",
    "            cnt += 1\n",
    "            A += 1 << 31\n",
    "        \n",
    "        while A:\n",
    "            if A & 1:   cnt += 1\n",
    "            A >>= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        bin_A = A & 0xffffffff\n",
    "        bin_B = B & 0xffffffff\n",
    "        count = bin(bin_A ^ bin_B).count('1')\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            res += (A>>i & 1)^(B>>i & 1)\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        A&=0xffffffff\n",
    "        B&=0xffffffff\n",
    "        return (A^B).bit_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        bin_a = A & (0xffffffff)\n",
    "        bin_b = B & (0xffffffff)\n",
    "        bin_final = bin(bin_a ^ bin_b).count('1')\n",
    "        return bin_final\n",
    "    \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            if A%2 != B%2:\n",
    "                res += 1\n",
    "            A >>= 1\n",
    "            B >>= 1\n",
    "        \n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        C = A ^ B\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            res += C>>i&1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        # A&0xFFFFFFFF，B&0xFFFFFFFF将A,B转化为无符号数\n",
    "        C = (A&0xFFFFFFFF) ^ (B&0xFFFFFFFF)\n",
    "        str_C = bin(C)\n",
    "        return str_C.count(\"1\")\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        C = A^B\n",
    "        for i in range(32):\n",
    "            if (C>>i)&1==1:\n",
    "                res+=1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        A=A&0xFFFFFFFF\n",
    "        B=B&0xFFFFFFFF\n",
    "        return (A^B).bit_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        A ^= B\n",
    "        cnt = 0\n",
    "        if A < 0:   \n",
    "            cnt += 1\n",
    "            A += 1 << 31\n",
    "        \n",
    "        while A:\n",
    "            if A & 1:   cnt += 1\n",
    "            A >>= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        n = 0\n",
    "        xor = A ^ B\n",
    "        for i in range(32):\n",
    "            if xor & (1 << i):\n",
    "                n += 1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')\n",
    "\n",
    "\n",
    "# 这个问题看似复杂，实则不难，我们发现只有该位置不同时才需要转换，因此想到使用异或得到结果 c，最后数一下 c 中 1 的个数即可。\n",
    "# 作者：腐烂的橘子\n",
    "# 链接：https://leetcode.cn/problems/convert-integer-lcci/solutions/191397/yi-huo-jie-fa-python-3-c-by-z1m/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def convertInteger(self, A: int, B: int) -> int:\n",
    "#         A=A&0xFFFFFFFF\n",
    "#         B=B&0xFFFFFFFF\n",
    "#         return (A^B).bit_count()\n",
    "\n",
    "### 网：方法1\n",
    "class Solution:\n",
    "    def convertInteger(self,A,B):\n",
    "        return bin((A&0xFFFFFFFF)^(B&0xFFFFFFFF)).count(\"1\") ### or str.bit_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        c = A ^ B\n",
    "        # print(c,c>>)\n",
    "        # ans = 0\n",
    "        # while(c):\n",
    "        #         c &=( c-1)\n",
    "        #         ans += 1\n",
    "        # return ans \n",
    "\n",
    "        for i in range(32):\n",
    "            res += c>>i  & 1\n",
    "            # c >>= 1\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        C = A ^ B\n",
    "        \n",
    "        count = 0\n",
    "        for i in range(32):\n",
    "            if C & (1<<i):\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def help(self, A, B):\n",
    "        if A < 0:           \n",
    "            rba = ''\n",
    "            A = -A      \n",
    "            ba = bin(A)[2:].zfill(32)      \n",
    "            for i in ba:\n",
    "                if i == '0':\n",
    "                    rba += '1'\n",
    "                else:\n",
    "                    rba += '0'\n",
    "            fba = ''\n",
    "            rrba = rba[::-1]\n",
    "            for j in range(len(rrba)):\n",
    "                if rrba[j] == '0':\n",
    "                    fba += '1' + rrba[j + 1:] \n",
    "                    break\n",
    "                else:\n",
    "                    fba += '0'\n",
    "                    \n",
    "            ba = fba[::-1]\n",
    "        else:\n",
    "            ba = bin(A)[2:].zfill(32)\n",
    "\n",
    "        return ba\n",
    "\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        \n",
    "          ba = self.help(A,B)      \n",
    "          bb = self.help(B,A)\n",
    "          \n",
    "          cnt = 0\n",
    "          for i in range(len(ba)):\n",
    "            if ba[i] != bb[i]:\n",
    "                cnt += 1\n",
    "          return cnt\n",
    "       \n",
    "            \n",
    "          \n",
    "      \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def convertInteger(self, A: int, B: int) -> int:\n",
    "        res = 0\n",
    "        c = A ^ B\n",
    "        # print(c,c>>)\n",
    "        for i in range(32):\n",
    "            res += c>>i  & 1\n",
    "            # c >>= 1\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
